<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-menu"></i>{{$route.name}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="handle-box">
            <el-dialog :title="title" :visible.sync="dialogFormVisible" class="expert">
                <el-form :model="addForm" inline>
                    <el-form-item label="名称" :label-width="formLabelWidth">
                        <el-input v-model="addForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="籍贯" :label-width="formLabelWidth">
                        <el-input v-model="addForm.nativePlace" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="专家称号" :label-width="formLabelWidth">
                        <el-input v-model="addForm.expertsTitles" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="学历" :label-width="formLabelWidth">
                        <el-input v-model="addForm.educational" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="排序" :label-width="formLabelWidth">
                        <el-input v-model="addForm.sortIndex" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="学位" :label-width="formLabelWidth">
                        <el-input v-model="addForm.degree" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="职称" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.position" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="职务" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.duty" auto-complete="off"></el-input>
                    </el-form-item>
                    <!-- <el-form-item label="学术论文" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.academicPaper" auto-complete="off"></el-input>
                    </el-form-item> -->
                    <el-form-item label="简历" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.resume" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="研究领域" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.research" auto-complete="off"></el-input>
                    </el-form-item>
                    <!-- <el-form-item label="背景教育" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.educational" auto-complete="off"></el-input>
                    </el-form-item> -->
                    <!-- <el-form-item label="内部报告" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.report" auto-complete="off"></el-input>
                    </el-form-item> -->
                    <!-- <el-form-item label="研究项目" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.researchProject" auto-complete="off"></el-input>
                    </el-form-item> -->
                    <el-form-item label="决策咨询成果" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.decisionConsultingResult" auto-complete="off"></el-input>
                    </el-form-item>

                    <el-form-item label="学术成果" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.academicWorks" auto-complete="off"></el-input>
                    </el-form-item>

                    <!-- <el-form-item label="学术著作" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.composition" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="学术兼职" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.partTimeJob" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="社会名誉" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.socialPrestige" auto-complete="off"></el-input>
                    </el-form-item> -->
                    <el-form-item label="成就" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.researchResult" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="所属机构" :label-width="formLabelWidth">
                        <el-select v-model="addForm.organizationId" placeholder="请选择所属机构">
                            <el-option v-for="org in orgs" :key="org.id" :label="org.name" :value="org.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="研究领域类型" :label-width="formLabelWidth">
                        <el-select v-model="addForm.fieldType" placeholder="请选择研究领域类型">
                            <el-option v-for="(type,index) in fieldTypes" :key="index" :label="type" :value="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="用户头像" :label-width="formLabelWidth">
                        <el-upload class="avatar-uploader" :data="uploadImg" ref="upload" :action="getLocation" :show-file-list="false" :on-success="handleAvatarSuccess" v-loading.fullscreen.lock="fullscreenLoading" :before-upload="beforeAvatarUpload">
                            <img v-if="addForm.img" :src="addForm.img" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>
                    <el-form-item label="性别" :label-width="formLabelWidth">
                        <el-select v-model="addForm.sex" placeholder="请选择研究领域类型">
                            <el-option v-for="(type,index) in ['男','女']" :key="index" :label="type" :value="type"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="出生日期" :label-width="formLabelWidth">
                        <el-date-picker v-model="addForm.dateOfBirth" type="date" value-format="yyyy年MM月dd日" placeholder="选择日期">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="是否隐藏">
                        <el-switch v-model="show" active-color="#13ce66">
                        </el-switch>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="cancel">取 消</el-button>
                    <el-button type="primary" @click="update">确 定</el-button>
                </div>
            </el-dialog>
            <el-input v-model="select_word" placeholder="输入专家名称" class="search"></el-input>
            <el-button type="primary" icon="search" @click="search">搜索</el-button>
            <el-button type="primary" @click="addExp">新增专家</el-button>
        </div>
        <div>
            <el-table :data="tableData" border fit style="width: 100%" v-loading="loading">
                <el-table-column label="头像" fixed>
                    <template slot-scope="scope">
                        <img :src="scope.row.img" width="100%" height="100%" />
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="名称" fixed>
                </el-table-column>
                <el-table-column prop="position" label="职级" show-overflow-tooltip empty-text>
                </el-table-column>
                <el-table-column prop="dateOfBirth" label="出生日期" show-overflow-tooltip empty-text>
                </el-table-column>
                <el-table-column prop="sex" label="性别" show-overflow-tooltip empty-text>
                </el-table-column>
                <el-table-column prop="resume" label="简历" show-overflow-tooltip empty-text>
                </el-table-column>
                <el-table-column prop="research" label="研究领域" show-overflow-tooltip empty-text>
                </el-table-column>
                <el-table-column prop="fieldType" label="研究领域类型" :formatter="type_format" show-overflow-tooltip empty-text>
                </el-table-column>
                <el-table-column prop="expertsTtles" label="专家称号" show-overflow-tooltip empty-text>
                </el-table-column>
                <el-table-column prop="educational" label="学历" show-overflow-tooltip empty-text>
                </el-table-column>
                <el-table-column prop="organizationId" :formatter="trans" label="所属机构" show-overflow-tooltip empty-text>
                </el-table-column>
                <el-table-column prop="nativePlace" label="籍贯" show-overflow-tooltip empty-text>
                </el-table-column>
                <el-table-column prop="duty" label="职务" show-overflow-tooltip empty-text>
                </el-table-column>
                <el-table-column prop="degree" label="学位" show-overflow-tooltip empty-text>
                </el-table-column>
                <el-table-column prop="decisionConsultingResult" label="决策咨询成果" show-overflow-tooltip empty-text>
                </el-table-column>
                <el-table-column prop="academicWorks" label="学术成果" show-overflow-tooltip empty-text>
                </el-table-column>
                <el-table-column prop="sortIndex" label="排序" show-overflow-tooltip empty-text>
                </el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <el-button @click="edit(scope.row)" type="text" size="small">编辑</el-button>
                        <el-button type="text" size="small" @click="delUser(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="block">
            <el-pagination @current-change="handleCurrentChange" :current-page="page.number+1" layout="total, sizes, prev, pager, next, jumper" :total="page.totalElements">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import { addExpert, delExpert, putExpert, allExpert } from "../../server/getDate.js";
import { mapState, mapActions } from 'vuex'
import { getStore, location } from '../../utils/local.js'
export default {
    data() {
        return {
            value: '',
            loading: false,
            dialogTableVisible: false,
            dialogFormVisible: false,
            dialogFormUpdate: false,
            select_word: '',
            title: '',
            getLocation: '',
            addForm: {},
            formLabelWidth: '120px',
            uploadImg: {},
            show: false,
            fullscreenLoading: false,
            fieldTypes: ['党建', '社会', '生态', '政治', '经济', '文化', '热点专题', '国际关系']
        }
    },
    created() {
        this.getExpert()
        this.getLocation = location
    },
    computed: mapState({
        page: state => state.Expert.result,
        tableData: state => state.Expert.result.content,
        orgs: state => state.Org.org,
        user: state => state.Login.user
    }),
    methods: {
        ...mapActions(['save_expert', 'add_expert', 'put_expert', 'del_expert']),
        type_format(row) {
            switch (row.fieldType) {
                case 0: return '党建'
                case 1: return '社会'
                case 2: return '生态'
                case 3: return '政治'
                case 4: return '经济'
                case 5: return '文化'
                case 6: return '热点专题'
                case 7: return '国际关系'
            }

        },
        getExpert() {
            allExpert().then(res => {
                if (res.status === 0) {
                    this.save_expert(res)
                }
            })
        },
        trans(row, col) {
            let orgs = this.orgs
            for (let i in orgs) {
                if (orgs[i].id === row.organizationId) {
                    return orgs[i].name
                }
            }
            return '未知'
        },
        getRoleName(item, id) {
            if (item.id === id) {
                console.log(item.name)
                return item.name
            }
        },
        handleClick(row) {
            console.log(row)
        },
        formatRole(row, col) {
            let roles = this.roles
            for (let i in roles) {
                if (roles[i].id === row.roleId) {
                    return roles[i].name
                }
            }
            return '未知'
        },
        search() {
            this.loading = true
            let index = this.page.number + 1
            this.handleCurrentChange(index)
        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            let field = 'name'
            let keyword = this.select_word
            let index = val
            let size = this.page.size
            allExpert({ field, keyword, index, size })
                .then(res => {
                    this.loading = false
                    if (res.status === 0) {
                        this.save_expert(res)
                    } else {
                        this.$message.error(res.message)
                    }
                }, err => this.$message.error('网络异常'))
        },
        delUser(row) {
            this.$confirm('此操作将永久删除该专家, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let userId = this.user.id
                let id = row.id
                delExpert({ id, userId })
                    .then(res => {
                        if (res.status === 0) {
                            console.log(res)
                            this.del_expert(id)
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }
                    })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },
        add() {
            this.addForm.shield = this.show ? 1 : 0
            let expertEntity = this.addForm
            console.log('add', expertEntity)
            let userId = this.user.id
            addExpert({ expertEntity, userId })
                .then(res => {
                    if (res.status === 0) {
                        this.dialogFormVisible = false
                        this.$message.info("添加成功")
                        this.getExpert()
                    } else {
                        this.$message.error(res.message)
                    }
                }, err => this.$message.error("网络异常"))
        },
        addExp() {
            this.dialogFormVisible = true
            this.title = '新增专家'
            this.addForm = {}
        },
        edit(row) {
            this.title = '修改专家'
            this.dialogFormVisible = true
            this.addForm = row
        },
        putExpert() {
            console.log('点击修改')
            let expertEntity = this.addForm
            let userId = this.user.id
            putExpert({ expertEntity, userId })
                .then(res => {
                    if (res.status === 0) {
                        this.dialogFormVisible = false
                        this.$message.info(res.message)
                        this.getExpert()
                    } else {
                        this.$message.error(res.message)
                    }
                }, err => this.$message.error(res.message))
        },
        handleAvatarSuccess(res, file) {
            this.fullscreenLoading = false
            this.addForm.img = res.result
        },
        beforeAvatarUpload(file) {
            return this.upload(file)
        },
        upload(file) {
            this.fullscreenLoading = true
            this.uploadImg.type = 0
            this.uploadImg.file = file
        },
        update() {
            if (this.addForm.id) {
                this.putExpert()
            } else {
                this.add()
            }
        },
        cancel() {
            this.dialogFormVisible = false
            this.getExpert()
        }
    }
}
</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 1%;
  .search {
    width: 30%;
  }
  .expert {
  }
}

.block {
  text-align: center;
  margin-top: 1%;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
